package ccc.virus.vedio.controller;


import java.util.Random;
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import sun.misc.BASE64Encoder;

import com.qcloud.vod.VodUploadClient;
import com.qcloud.vod.model.VodUploadRequest;
import com.qcloud.vod.model.VodUploadResponse;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("vedio")
public class VedioController {

    private String secretId;
    private String secretKey;
    private long currentTime;
    private int random;
    private int signValidDuration;

    private static final String HMAC_ALGORITHM = "HmacSHA1";
    private static final String CONTENT_CHARSET = "UTF-8";

    private Log log = LogFactory.getLog(VedioController.class);

    @RequestMapping
    public String redio(){
        return "vedio";
    }

    @RequestMapping("upload")
    public String upload(){
        return "upload";
    }

    @RequestMapping("uploadFile")
    @ResponseBody
    public String uploadFile(){
        VodUploadClient client = new VodUploadClient("your secretId", "your secretKey");

        VodUploadRequest request = new VodUploadRequest();
        request.setMediaFilePath("C:\\Users\\bruin\\Desktop\\fKMuL0wIaJ8A.mp4");

        try {
            VodUploadResponse response = client.upload("ap-guangzhou", request);
            System.out.println(response.getFileId());
        } catch (Exception e) {
            // 业务方进行异常处理
            log.error("Upload Err", e);
        }


        return null;
    }

    @RequestMapping("ajaxGetSign")
    @ResponseBody
    public String ajaxGetSign(){
        secretId = "AKID5L2tU0moBSkstlbYIE6syIUlz54YuIG6";
        secretKey = "tgIyTmurdBbqzPisqbAXkmfLtLE2fBUb";
        currentTime = System.currentTimeMillis() / 1000;
        random = new Random().nextInt(java.lang.Integer.MAX_VALUE);
        signValidDuration = 3600 * 24 * 2;

        try {
            String signature = getUploadSignature();
            System.out.println("signature : " + signature);
            return signature;
        } catch (Exception e) {
            System.out.print("获取签名失败");
            e.printStackTrace();
        }
        return "fail";
    }

    public String getUploadSignature() throws Exception {
        String strSign = "";
        String contextStr = "";

        long endTime = (currentTime + signValidDuration);
        contextStr += "secretId=" + java.net.URLEncoder.encode(secretId, "utf8");
        contextStr += "&currentTimeStamp=" + currentTime;
        contextStr += "&expireTime=" + endTime;
        contextStr += "&random=" + random;

        try {
            Mac mac = Mac.getInstance(HMAC_ALGORITHM);
            SecretKeySpec secretKey = new SecretKeySpec(this.secretKey.getBytes(CONTENT_CHARSET), mac.getAlgorithm());
            mac.init(secretKey);

            byte[] hash = mac.doFinal(contextStr.getBytes(CONTENT_CHARSET));
            byte[] sigBuf = byteMerger(hash, contextStr.getBytes("utf8"));
            strSign = new String(new BASE64Encoder().encode(sigBuf).getBytes());
            strSign = strSign.replace(" ", "").replace("\n", "").replace("\r", "");
        } catch (Exception e) {
            throw e;
        }
        return strSign;
    }

    public static byte[] byteMerger(byte[] byte1, byte[] byte2) {
        byte[] byte3 = new byte[byte1.length + byte2.length];
        System.arraycopy(byte1, 0, byte3, 0, byte1.length);
        System.arraycopy(byte2, 0, byte3, byte1.length, byte2.length);
        return byte3;
    }




}
